package cn.yyh.servicePet.controller;

import cn.yyh.commons.exception.MyException;
import cn.yyh.commons.utils.GetTokenUtil;
import cn.yyh.commons.utils.PageInfo;
import cn.yyh.commons.utils.R;
import cn.yyh.servicePet.domain.Pet;
import cn.yyh.servicePet.domain.SearchMasterMsg;
import cn.yyh.servicePet.query.SearchMasterMsgQuery;
import cn.yyh.servicePet.service.ISearchMasterMsgService;
import cn.yyh.servicePet.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: SearchMasterMsgController
 * @Package: cn.yyh.pet.controller
 * @date 2021/7/5
 */
@RestController
@RequestMapping("/servicePet/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    @GetMapping
    public R listAll(){
        try {
            List<SearchMasterMsg> list = seachMasterMsgService.listAll();
            return R.oK().data("rows",list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/publish")
    public R publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            seachMasterMsgService.publish(searchMasterMsg,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("/queryPage")
    public R queryPets(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            PageInfo<SearchMasterMsg> info = seachMasterMsgService.queryPage(query,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK().data("rows", info.getRows()).data("total",info.getTotals());
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }

    @PostMapping("/handle")
    public R handlePets(@RequestBody Pet pet, HttpServletRequest request){
        try {
            seachMasterMsgService.handlePets(pet,
                    Objects.requireNonNull(ContextUtil.getLoginInfo(request)).getId(),
                    GetTokenUtil.getToken(request));
            return R.oK();
        } catch (MyException e) {
            e.printStackTrace();
            return R.error().message(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("系统繁忙");
        }
    }


}
